package and.calc;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View.OnClickListener;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class ProgrammerViewClass extends Activity 
{

	Button num0;
	Button num1;
	Button num2;
	Button num3;
	Button num4;
	Button num5;
	Button num6;
	Button num7;
	Button num8;
	Button num9;
	Button numA;
	Button numB;
	Button numC;
	Button numD;
	Button numE;
	Button numF;
	Button addButt;
	Button minButt;
	Button mulButt;
	Button divButt;
	Button equButt;
	Button andButt;
	Button orButt;
	Button notButt;
	Button xorButt;
	String enteredText;
	boolean operationSet;
	Format numberFormat;
	Format oldNumberFormat;
	EditText calcText;
	Calculator calc;
	
	
	// This Function set the Layout of this view and buttons functionalities
	@Override
	protected void onCreate(Bundle savedInstanceState) { 
		super.onCreate(savedInstanceState);
		setContentView(R.layout.programmerview);
		
		calc = new Calculator();
		calcText = (EditText) findViewById(R.id.simpCalcText);
		numberFormat = Format.DEC;
		oldNumberFormat = numberFormat;
		

	
		// Implement Click Functions For Buttons
        // 1- Implement Functions For Digits(0-F) 
        num0 = (Button) findViewById(R.id.num0);
        num0.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				enteredText = calcText.getText().toString(); 
				
				if(enteredText.length() == 1)
				{
					if(!enteredText.contains("0"))
					{
						if(operationSet)
							calcText.setText("0");
						else
							calcText.setText(calcText.getText() + "0");						
					}
				}
				else
				{
					if(operationSet)
						calcText.setText("0");
					else
						calcText.setText(calcText.getText() + "0");
				}												
			}
		});
        
        num1 = (Button) findViewById(R.id.num1);
        num1.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				enteredText = calcText.getText().toString(); 
				
				if(((enteredText.length() == 1) && (enteredText.contains("0"))) || (operationSet))
				{
					calcText.setText("1");			
					operationSet = false;
				}
				else
				{
					calcText.setText(calcText.getText() + "1");
				}			
			}
		});
        
        
        num2 = (Button) findViewById(R.id.num2);
        num2.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				enteredText = calcText.getText().toString(); 
				
				if(((enteredText.length() == 1) && (enteredText.contains("0"))) || (operationSet))
				{
					calcText.setText("2");
					operationSet = false;
				}
				else
				{
					calcText.setText(calcText.getText() + "2");
				}					
			}
		});
        
        num3 = (Button) findViewById(R.id.num3);
        num3.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				enteredText = calcText.getText().toString(); 
				
				if(((enteredText.length() == 1) && (enteredText.contains("0"))) || (operationSet))
				{
					calcText.setText("3");
					operationSet = false;
				}
				else
				{
					calcText.setText(calcText.getText() + "3");
				}
							
			}
		});
        
        num4 = (Button) findViewById(R.id.num4);
        num4.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				enteredText = calcText.getText().toString(); 
				
				if(((enteredText.length() == 1) && (enteredText.contains("0"))) || (operationSet))
				{
					calcText.setText("4");
					operationSet = false;
				}
				else
				{
					calcText.setText(calcText.getText() + "4");
				}			
			}
		});
        
        num5 = (Button) findViewById(R.id.num5);
        num5.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				enteredText = calcText.getText().toString(); 
				
				if(((enteredText.length() == 1) && (enteredText.contains("0"))) || (operationSet))
				{
					calcText.setText("5");
					operationSet = false;
				}
				else
				{
					calcText.setText(calcText.getText() + "5");
				}			
			}
		});
        
        num6 = (Button) findViewById(R.id.num6);
        num6.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				enteredText = calcText.getText().toString(); 
				
				if(((enteredText.length() == 1) && (enteredText.contains("0"))) || (operationSet))
				{
					calcText.setText("6");
					operationSet = false;
				}
				else
				{
					calcText.setText(calcText.getText() + "6");
				}			
			}
		});
        
        num7 = (Button) findViewById(R.id.num7);
        num7.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				enteredText = calcText.getText().toString(); 
				
				if(((enteredText.length() == 1) && (enteredText.contains("0"))) || (operationSet))
				{
					calcText.setText("7");
					operationSet = false;
				}
				else
				{
					calcText.setText(calcText.getText() + "7");
				}		
			}
		});
        
        num8 = (Button) findViewById(R.id.num8);
        num8.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				enteredText = calcText.getText().toString(); 
				
				if(((enteredText.length() == 1) && (enteredText.contains("0"))) || (operationSet))
				{
					calcText.setText("8");
					operationSet = false;
				}
				else
				{
					calcText.setText(calcText.getText() + "8");
				}			
			}
		});
        
        num9 = (Button) findViewById(R.id.num9);
        num9.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				enteredText = calcText.getText().toString(); 
				
				if(((enteredText.length() == 1) && (enteredText.contains("0"))) || (operationSet))
				{
					calcText.setText("9");
					operationSet = false;
				}
				else
				{
					calcText.setText(calcText.getText() + "9");
				}			
			}
		});

        numA = (Button) findViewById(R.id.numA);
        numA.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				enteredText = calcText.getText().toString(); 
				
				if(((enteredText.length() == 1) && (enteredText.contains("0"))) || (operationSet))
				{
					calcText.setText("A");
					operationSet = false;
				}
				else
				{
					calcText.setText(calcText.getText() + "A");
				}			
			}
		});
        
        numB = (Button) findViewById(R.id.numB);
        numB.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				enteredText = calcText.getText().toString(); 
				
				if(((enteredText.length() == 1) && (enteredText.contains("0"))) || (operationSet))
				{
					calcText.setText("B");
					operationSet = false;
				}
				else
				{
					calcText.setText(calcText.getText() + "B");
				}			
			}
		});
        
        numC = (Button) findViewById(R.id.numC);
        numC.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				enteredText = calcText.getText().toString(); 
				
				if(((enteredText.length() == 1) && (enteredText.contains("0"))) || (operationSet))
				{
					calcText.setText("C");
					operationSet = false;
				}
				else
				{
					calcText.setText(calcText.getText() + "C");
				}			
			}
		});
        
        numD = (Button) findViewById(R.id.numD);
        numD.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				enteredText = calcText.getText().toString(); 
				
				if(((enteredText.length() == 1) && (enteredText.contains("0"))) || (operationSet))
				{
					calcText.setText("D");
					operationSet = false;
				}
				else
				{
					calcText.setText(calcText.getText() + "D");
				}			
			}
		});
				
        numE = (Button) findViewById(R.id.numE);
        numE.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				enteredText = calcText.getText().toString(); 
				
				if(((enteredText.length() == 1) && (enteredText.contains("0"))) || (operationSet))
				{
					calcText.setText("E");
					operationSet = false;
				}
				else
				{
					calcText.setText(calcText.getText() + "E");
				}			
			}
		});
        
        numF = (Button) findViewById(R.id.numF);
        numF.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				enteredText = calcText.getText().toString(); 
				
				if(((enteredText.length() == 1) && (enteredText.contains("0"))) || (operationSet))
				{
					calcText.setText("F");
					operationSet = false;
				}
				else
				{
					calcText.setText(calcText.getText() + "F");
				}			
			}
		});
        
    
        // Set the DEC normal View
        num0.setEnabled(true);
        num1.setEnabled(true);
        num2.setEnabled(true);
        num3.setEnabled(true);
        num4.setEnabled(true);
        num5.setEnabled(true);
        num6.setEnabled(true);
        num7.setEnabled(true);
        num8.setEnabled(true);
        num9.setEnabled(true);
        
        numA.setEnabled(false);
        numB.setEnabled(false);
        numC.setEnabled(false);
        numD.setEnabled(false);
        numE.setEnabled(false);
        numF.setEnabled(false);
        
        
        
        // Set the Functions Buttons
        addButt = (Button) findViewById(R.id.addSign);
        addButt.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				enteredText = calcText.getText().toString();
				
				calc.setM_number1WithFormat(enteredText, numberFormat);
				calc.setOperation(Operations.add);
				
				operationSet = true;
				
			}
		});
        
        
        minButt = (Button) findViewById(R.id.minusSign);
        minButt.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				enteredText = calcText.getText().toString();
				
				calc.setM_number1WithFormat(enteredText, numberFormat);
				calc.setOperation(Operations.sub);
				
				operationSet = true;
				
			}
		});
        

        mulButt = (Button) findViewById(R.id.multSign);
        mulButt.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
			
				enteredText = calcText.getText().toString();
				
				calc.setM_number1WithFormat(enteredText, numberFormat);
				calc.setOperation(Operations.mul);
				
				operationSet = true;
				
			}
		});
        
        
        divButt = (Button) findViewById(R.id.divSign);
        divButt.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
			
				enteredText = calcText.getText().toString();
				
				calc.setM_number1WithFormat(enteredText, numberFormat);
				calc.setOperation(Operations.div);
				
				operationSet = true;
			}
		});
        
        
        andButt = (Button) findViewById(R.id.andSign);
        andButt.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				enteredText = calcText.getText().toString();
				
				calc.setM_number1WithFormat(enteredText, numberFormat);
				calc.setOperation(Operations.and);
				
				operationSet = true;
				
			}
		});
        
        
        orButt = (Button) findViewById(R.id.orSign);
        orButt.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				enteredText = calcText.getText().toString();
				
				calc.setM_number1WithFormat(enteredText, numberFormat);
				calc.setOperation(Operations.or);
				
				operationSet = true;
				
			}
		});
        
        
        notButt = (Button) findViewById(R.id.notSign);
        notButt.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				enteredText = calcText.getText().toString();
				
				calc.setM_number1WithFormat(enteredText, numberFormat);
				calc.setOperation(Operations.not);
				
				operationSet = true;
				
			}
		});
        
        
        xorButt = (Button) findViewById(R.id.xorSign);
        xorButt.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				enteredText = calcText.getText().toString();
				
				calc.setM_number1WithFormat(enteredText, numberFormat);
				calc.setOperation(Operations.xor);
				
				operationSet = true;
				
			}
		});
        
        
        
        equButt = (Button) findViewById(R.id.equalSign);
        equButt.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				enteredText = calcText.getText().toString();
				calc.setM_number2WithFormat(enteredText, numberFormat);
				calcText.setText(calc.getM_resultWithFormat(numberFormat));
			}
		});

        
	}

	
	   
    ///// Menu Handling 
    
	// This Function set the Options Menu to be used with this activity
    @Override
	public boolean onCreateOptionsMenu(Menu menu) {
		
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.programmer_menu, menu);
    	
		return true;
	}

    
	// This Function Check the item corresponding to current Format
	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {

		super.onPrepareOptionsMenu(menu);
		
		switch (numberFormat)
		{
		case HEX:
			menu.findItem(R.id.hexFormat).setChecked(true);		
			break;
			
		case BIN:
			menu.findItem(R.id.binFormat).setChecked(true);
			break;
			
		case DEC:
			menu.findItem(R.id.decFormat).setChecked(true);
			break;

		case OCT:
			menu.findItem(R.id.octFormat).setChecked(true);;
			break;
		}			
		
		return true;
		
		
	}


	// This Function implements the handling of choosing a menu Item
	// Set the View and convert the current number format
	@Override
	public boolean onOptionsItemSelected(MenuItem item) 
	{
		
		Log.d("Calculator", " Menu Item ID = " +  String.valueOf(item.getItemId() ));
		Log.d("Calculator", "Item Menu Title = " + item.getTitle().toString());

		
		switch(item.getItemId())
		{
			case R.id.binFormat:
			
				num0.setEnabled(true);
				num1.setEnabled(true);			
				num2.setEnabled(false);
				num3.setEnabled(false);
				num4.setEnabled(false);
				num5.setEnabled(false);
				num6.setEnabled(false);
				num7.setEnabled(false);
				num8.setEnabled(false);
				num9.setEnabled(false);
				numA.setEnabled(false);
				numB.setEnabled(false);
				numC.setEnabled(false);
				numD.setEnabled(false);
				numE.setEnabled(false);
				numF.setEnabled(false);
				
				oldNumberFormat = numberFormat;
				numberFormat = Format.BIN;
				changeTextNumberFomrat();
				
				break;
				
			case R.id.octFormat:
				
			
				num0.setEnabled(true);
				num1.setEnabled(true);
				num2.setEnabled(true);
				num3.setEnabled(true);
				num4.setEnabled(true);
				num5.setEnabled(true);
				num6.setEnabled(true);
				num7.setEnabled(true);			
				num8.setEnabled(false);
				num9.setEnabled(false);
				numA.setEnabled(false);
				numB.setEnabled(false);
				numC.setEnabled(false);
				numD.setEnabled(false);
				numE.setEnabled(false);
				numF.setEnabled(false);
				
				oldNumberFormat = numberFormat;
				numberFormat = Format.OCT;
				changeTextNumberFomrat();
				
				break;
				
			case R.id.decFormat:
		
				num0.setEnabled(true);
				num1.setEnabled(true);
				num2.setEnabled(true);
				num3.setEnabled(true);
				num4.setEnabled(true);
				num5.setEnabled(true);
				num6.setEnabled(true);
				num7.setEnabled(true);			
				num8.setEnabled(true);
				num9.setEnabled(true);
				numA.setEnabled(false);
				numB.setEnabled(false);
				numC.setEnabled(false);
				numD.setEnabled(false);
				numE.setEnabled(false);
				numF.setEnabled(false);
				
				oldNumberFormat = numberFormat;
				numberFormat = Format.DEC;
				changeTextNumberFomrat();
				
				break;
				
			case R.id.hexFormat:
		
				num0.setEnabled(true);
				num1.setEnabled(true);
				num2.setEnabled(true);
				num3.setEnabled(true);
				num4.setEnabled(true);
				num5.setEnabled(true);
				num6.setEnabled(true);
				num7.setEnabled(true);			
				num8.setEnabled(true);
				num9.setEnabled(true);			
				numA.setEnabled(true);
				numB.setEnabled(true);
				numC.setEnabled(true);
				numD.setEnabled(true);
				numE.setEnabled(true);
				numF.setEnabled(true);
				
				oldNumberFormat = numberFormat;
				numberFormat = Format.HEX;
				changeTextNumberFomrat();
				
				break;
		
		
			case R.id.clearItem:
		
				calcText.setText("0");
			
				calc.setM_number1WithFormat("0", numberFormat);
				calc.setM_number2WithFormat("0", numberFormat);
				calc.setOperation(Operations.none);
				
				break;
				
			case R.id.removeItem:
		
				int length = calcText.getText().toString().length();
				
				
				if(length == 0)
				{
					// Do Nothing
				}
				else if(length == 1)
				{		
					calcText.setText("0");				
				}
				else
				{
					calcText.setText(calcText.getText().toString().substring(0, calcText.getText().toString().length()-1));
				}
				
				break;
		}
		
		
		return true;
	}
    	
	
	// This Function Convert Numbers between Different Formats 
	// Decimal, Hex-decimal, Octal and Binary 
	private void changeTextNumberFomrat()
	{
		String oldNumber = calcText.getText().toString();
		String newNumber = "";
		
		switch(oldNumberFormat)
		{
		case HEX:
			
			switch (numberFormat)
			{
			case DEC:
				newNumber = calc.convertHEXtoDEC(oldNumber);
				break;
				
			case BIN:
				newNumber = calc.convertHEXtoBIN(oldNumber);
				break;
				
			case OCT:
				newNumber = calc.convertHEXtoOCT(oldNumber);
				
			case HEX:
				break;
			}
					
			break;
			
		case DEC:
			
			switch(numberFormat)
			{
			case HEX:
				newNumber = calc.convertDECtoHEX(oldNumber);
				break;
				
			case BIN:
				newNumber= calc.convertDECtoBIN(oldNumber);
				break;
				
			case OCT:
				newNumber = calc.convertDECtoOCT(oldNumber);
				break;
				
			case DEC:
				break;
			
			}
			
			break;
			
		case BIN:
			
			switch(numberFormat)
			{
			case HEX:
				newNumber = calc.convertBINtoHEX(oldNumber);
				break;
				
			case DEC:
				newNumber = calc.convertBINtoDEC(oldNumber);
				break;
				
			case OCT:
				newNumber = calc.convertBINtoOCT(oldNumber);
				break;
				
			case BIN:				
				break;
			}
			
			break;
			
		case OCT:
			
			switch(numberFormat)
			{
			case HEX:
				newNumber = calc.convertOCTtoHEX(oldNumber);
				break;
				
			case DEC:
				newNumber = calc.convertOCTtoDEC(oldNumber);
				break;
				
			case BIN:
				newNumber = calc.convertOCTtoBIN(oldNumber);
				break;
				
			case OCT:
			}
			
			break;
		}
		
		calcText.setText(newNumber.toUpperCase());
	}
   
	
	
}
